#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=4005,M=70;
ll row[M][M][M],col[M][M][M],Pcol[M][M][M],Prow[M][M][M];
int L[N],R[N],blo[N],a[N][N],D[N];
int n,q,op,dir,r,x,y,B;
int main(){
	freopen("fire.in","r",stdin);
	freopen("fire.out","w",stdout);
	scanf("%d%d",&n,&q); B=sqrt(n);
	for (int i=1; i<=n; i++){
		blo[i]=(i-1)/B+1; R[blo[i]]=i;
		if (blo[i]!=blo[i-1]) L[blo[i]]=i;
	}
	for (int i=1; i<=n; i++) D[i]=i-L[blo[i]]+1;
	for (; q; q--){
		scanf("%d",&op);
		if (op==1){
			scanf("%d%d%d%d",&dir,&r,&op,&x);
			if (dir==0){
				for (int i=1; i<=n; i++){
					int las=a[r][i];
					if (op==1) a[r][i]&=x;
					else if (op==2) a[r][i]|=x;
					else if (op==3) a[r][i]^=x;
					else a[r][i]=(a[r][i]+x)%999999999;
					row[blo[r]][blo[i]][D[r]]+=a[r][i]-las;
					col[blo[r]][blo[i]][D[i]]+=a[r][i]-las;
				}
				for (int i=1; i<=blo[n]; i++){
					int len=R[blo[r]]-L[blo[r]]+1;
					for (int j=1; j<=len; j++) Prow[blo[r]][i][j]=Prow[blo[r]][i][j-1]+row[blo[r]][i][j];
					len=R[i]-L[i]+1;
					for (int j=1; j<=len; j++) Pcol[blo[r]][i][j]=Pcol[blo[r]][i][j-1]+col[blo[r]][i][j];
				}
			} else {
				for (int i=1; i<=n; i++){
					int las=a[i][r];
					if (op==1) a[i][r]&=x;
					else if (op==2) a[i][r]|=x;
					else if (op==3) a[i][r]^=x;
					else a[i][r]=(a[i][r]+x)%999999999;
					row[blo[i]][blo[r]][D[i]]+=a[i][r]-las;
					col[blo[i]][blo[r]][D[r]]+=a[i][r]-las;
				}
				for (int i=1; i<=blo[n]; i++){
					int len=R[blo[r]]-L[blo[r]]+1;
					for (int j=1; j<=len; j++) Pcol[i][blo[r]][j]=Pcol[i][blo[r]][j-1]+col[i][blo[r]][j];
					len=R[i]-L[i]+1;
					for (int j=1; j<=len; j++) Prow[i][blo[r]][j]=Prow[i][blo[r]][j-1]+row[i][blo[r]][j];
				}
			}
		} else {
			scanf("%d%d",&x,&y); ll ans=0;
			for (int i=1; i<blo[x]; i++)
				for (int j=1; j<blo[y]; j++) ans+=Prow[i][j][R[i]-L[i]+1];
			for (int i=1; i<blo[x]; i++) ans+=Pcol[i][blo[y]][D[y]];
			for (int i=1; i<blo[y]; i++) ans+=Prow[blo[x]][i][D[x]];
			for (int i=L[blo[x]]; i<=x; i++)
				for (int j=L[blo[y]]; j<=y; j++) ans+=a[i][j];
			printf("%lld\n",ans);
		}
	}
}
